<!DOCTYPE html>
<html>
<head>
    <title>添加 {{ table_name }} 记录</title>
    <link href="/Style/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/Style/css/bootstrap-icons.css">
    <style>
        body {
            padding: 2rem;
            background-color: #f8f9fa;
        }
        .form-group {
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1><i class="bi bi-plus-circle me-2"></i>添加 {{ table_name }} 记录</h1>
            <div>
                <!-- 修复：添加蓝图前缀 sqlmanager_bp. -->
                <a href="{{ url_for('sqlmanager_bp.table_view', table_name=table_name) }}" class="btn btn-outline-secondary">
                    <i class="bi bi-arrow-left me-1"></i> 返回列表
                </a>
            </div>
        </div>

        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <div class="card shadow-sm">
            <div class="card-body">
                <!-- 修复：添加蓝图前缀 sqlmanager_bp. -->
                <form action="{{ url_for('sqlmanager_bp.add_record', table_name=table_name) }}" method="post">
                    <div class="row">
                        {% for field in structure %}
                        <div class="col-md-6 mb-3">
                            <label for="{{ field.name }}" class="form-label">
                                {{ field.name }}
                                {% if field.is_primary %}
                                    <span class="badge bg-primary ms-1">主键</span>
                                {% endif %}
                                {% if not field.nullable and not field.is_primary %}
                                    <span class="text-danger">*</span>
                                {% endif %}
                                <small class="d-block text-muted">{{ field.type }}</small>
                            </label>

                            {% if field.is_primary and field.get('autoincrement', False) %}
                                <input type="text" class="form-control bg-light" value="自动生成" readonly>
                                <input type="hidden" name="{{ field.name }}" value="">
                            {% else %}
                                <input type="text"
                                       class="form-control {% if field.is_primary %}bg-light{% endif %}"
                                       id="{{ field.name }}"
                                       name="{{ field.name }}"
                                       {% if not field.nullable and not field.is_primary %}required{% endif %}
                                       {% if field.is_primary %}readonly{% endif %}>
                            {% endif %}

                            {% if not field.nullable and not field.is_primary %}
                                <div class="form-text text-danger">此字段为必填项</div>
                            {% endif %}
                        </div>
                        {% endfor %}
                    </div>

                    <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">
                        <button type="submit" class="btn btn-success">
                            <i class="bi bi-save me-1"></i> 保存记录
                        </button>
                        <!-- 修复：添加蓝图前缀 sqlmanager_bp. -->
                        <a href="{{ url_for('sqlmanager_bp.table_view', table_name=table_name) }}" class="btn btn-outline-secondary">
                            <i class="bi bi-x-circle me-1"></i> 取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="/Style/js/bootstrap.bundle.min.js"></script>
</body>
</html>